﻿using RedLockNet;
using RedLockNet.SERedis;
using RedLockNet.SERedis.Configuration;
using Service.Core.Redis.Services;
using StackExchange.Redis;

namespace Service.Core.Redis.DistributedLock
{
    public class DistributedLockService : IDistributedLockService
    {
        private readonly RedLockFactory _redLockFactory;
        /// <summary>
        /// 
        /// </summary>
        public DistributedLockService(IRedisService redisService)
        {
            var multiplexers = new List<RedLockMultiplexer> { (ConnectionMultiplexer)redisService.Multiplexer };
            _redLockFactory = RedLockFactory.Create(multiplexers);
        }
        public async Task<IRedLock> AcquireLockAsync(string resource, TimeSpan expiryTime, TimeSpan waitTime, TimeSpan retryTime)
        {
            var _redLock = await _redLockFactory.CreateLockAsync(resource, expiryTime, waitTime, retryTime);
            return _redLock;
        }
    }
}
